using UnityEngine;
using System.IO;
using System.Collections;
using System.Xml;

public class GetText {
	
	public XmlDocument[] Portuguese = new XmlDocument[2];
	public XmlDocument[] English = new XmlDocument[2];
	private XmlDocument currentLanguage;
	private static GetText _instance;
	string text;
	
	private XmlDocument document;
	
	private GetText()
	{
		for (int i = 0; i < 2; i++) 
		{
		
			English[i] = new XmlDocument();
			Portuguese[i] = new XmlDocument();
		}
		
		TextAsset PC_01 = (TextAsset) Resources.Load ("PC/English/English");
		TextAsset PC_02 = (TextAsset) Resources.Load ("PC/Portugues/Portugues");
		TextAsset ANDROID_01 = (TextAsset) Resources.Load ("Android/English/English");
		TextAsset ANDROID_02 = (TextAsset) Resources.Load ("Android/Portugues/Portugues");
		
		English[0].LoadXml(PC_01.text);
		Portuguese[0].LoadXml(PC_02.text);
		English[1].LoadXml(ANDROID_01.text);
		Portuguese[1].LoadXml(ANDROID_02.text);
	}
	
	public void SetLanguages(XmlDocument[] englishPath,XmlDocument[] portuguesePath)
	{
		English = englishPath;
		Portuguese = portuguesePath;
	}
	
	static public GetText getInstance()
	{
		if(_instance == null)
			_instance = new GetText();
		
		return _instance;
	}
	
	public string returnText(string id)
	{
		string language = PlayerPrefs.GetString("Language");
		#if UNITY_ANDROID
		if(language.Equals("English"))
		{
			currentLanguage = English[1];
		}
		if(language.Equals("Portugues"))
		{
			currentLanguage = Portuguese[1];
		}
		#endif
		#if UNITY_STANDALONE
		
		if(language.Equals("English"))
		{
			currentLanguage = English[0];
		}
		if(language.Equals("Portugues"))
		{
			currentLanguage = Portuguese[0];
		}
		#endif
		
		return getText	(id);
	}
		
	private string getText(string id)
	{
		text = "";
		XmlDocument xmlDoc = new XmlDocument();
		xmlDoc = currentLanguage;
		XmlNodeList xmlList = xmlDoc.GetElementsByTagName(PlayerPrefs.GetString("Language"));
		
		foreach (XmlNode xn in xmlList)
		{
			if(!xn[id].IsEmpty)
			{
				text += xn[id].InnerText;
				break;
			}
		}

		return text;
		
	}
	
	#region README
	//Nas Linguagens, o Elemento:
	//0 - PC
	//1 - Android
	#endregion
	
}
